package com.zygame.sdk.view.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zygame.sdk.ConstantValue;
import com.zygame.sdk.ZyGameSDK;
import com.zygame.sdk.model.UserModel;
import com.zygame.sdk.utils.phone.Phoneuitl;
import com.zygame.sdk.utils.res.ResourceUtil;

import java.util.Locale;

/**
 * Yuxl
 * Dialog 基类
 */

//public abstract class BaseDialog extends Dialog implements OnDismissListener{
public abstract class ZY_BaseDialog extends AlertDialog implements OnDismissListener, OnKeyListener, View.OnClickListener {

    public Context mContext;
    private int mWidth;
    private int mHeight;
    protected TextView btn_back;
    protected ImageView btn_close;
    private ProgressDialog progressDialog;
    private RelativeLayout rl_head;

    @SuppressLint("NewApi")
    public ZY_BaseDialog(Context context) {
        super(context, ResourceUtil.getStyleId(context, "actionSheet"));

        this.setCanceledOnTouchOutside(false);
        this.mContext = context;
        this.setCancelable(false);

        int w = Phoneuitl.getWpixels(context);
        int h = Phoneuitl.getHpixels(context);


        if (ZyGameSDK.getInstance().getOrientation()) {   //横屏
            this.mWidth = 1400 / 3;
            this.mHeight = 870 / 3;
        } else {

            if ((450 < w && w < 500) && (h > 800 && h < 900)) {    // 480x854
                this.mWidth = 300;
//				this.mHeight = (int)(mWidth * 0.618)+60;
//                this.mHeight = (int) (mWidth * 0.618) + 90;
            } else {
                this.mWidth = 355;
//				this.mHeight = (int)(mWidth * 0.618) + 30;
//                this.mHeight = (int) (mWidth * 0.618) + 40;
            }
        }
//		int arr[] = Phoneuitl.getScreenWidthHeight((Activity)context);
//		System.out.println("width:height = " + arr[0] + "---" +arr[1]);
//		System.out.println("mHeight = " + this.mHeight + "   this.mWidth = " + this.mWidth);

    }

    public ZY_BaseDialog(Context context, int width, int height) {
        super(context, ResourceUtil.getStyleId(context, "actionSheet"));
        this.setCanceledOnTouchOutside(false);
        this.mContext = context;
        this.mWidth = width;
//        this.mHeight = height;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Resources resources = mContext.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = resources.getConfiguration();
        try {
            if (ConstantValue.ZY_LANGUAGE.equals("1000") ){
                // 应用用户选择语言
                config.locale = Locale.ENGLISH;
            } else {
                config.locale = Locale.CHINESE;
            }

            resources.updateConfiguration(config, dm);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        setContentView(ResourceUtil.getLayoutId(mContext, "zy_basedialog"));
        progressDialog = new ProgressDialog(mContext);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("加载中，请稍候...");
        initView();
        LayoutParams params = this.getWindow().getAttributes();
        float density = mContext.getResources().getDisplayMetrics().density;
        params.width = (int) (density * mWidth);
//        params.height = (int) (density * mHeight);

        this.setOnDismissListener(this);
        this.setOnKeyListener(this);
    }

    private void initView() {
        rl_head = (RelativeLayout) findViewById(ResourceUtil.getId(mContext, "rl_head"));
        LinearLayout ll_add = (LinearLayout) findViewById(ResourceUtil.getId(mContext, "ll_add"));
        btn_back = (TextView) findViewById(ResourceUtil.getId(mContext, "btn_back"));
        btn_close = (ImageView) findViewById(ResourceUtil.getId(mContext, "btn_close"));
        View view = LayoutInflater.from(mContext).inflate(ResourceUtil.getLayoutId(mContext, getResourceStr()), null);
        ll_add.addView(view);
        init();
        btn_back.setOnClickListener(this);
        btn_close.setOnClickListener(this);
    }

    protected abstract void init();

    protected abstract String getResourceStr();


    protected void setDismiss() {
        this.dismiss();
    }

//	@Override
//	public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
//		return false;
//	}

    @Override
    public void onDismiss(DialogInterface arg0) {
//        int flag = ZyGameSDK.getInstance().dialogDismissFlag;
//        switch (flag) {
//            case CmCode.CODE_LOGIN_CANCEL:
//                ZyGameSDK.getInstance().onResult(CmCode.CODE_LOGIN_CANCEL, "65sdk login cancel");
//                break;
//            case CmCode.CODE_LOGIN_FAIL:
//                ZyGameSDK.getInstance().onResult(CmCode.CODE_LOGIN_FAIL, "65sdk login fail");
//                break;
//            case CmCode.CODE_REGISTER_CANCEL:
//                CmGameSDK.getInstance().onResult(CmCode.CODE_REGISTER_CANCEL, "65sdk register_cancel");
//                break;
//            case CmCode.CODE_REGISTER_FAIL:
//                CmGameSDK.getInstance().onResult(CmCode.CODE_REGISTER_CANCEL, "65sdk register_fail");
//                break;
//            default:
//                break;
//        }
//        CmGameSDK.getInstance().setDialogDismissFlag(0);
    }

    @Override
    public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
//        CmGameSDK.getInstance().onResult(CmCode.CODE_LOGIN_CANCEL, "65sdk login cancel");
        return false;
    }


    protected void showBack(boolean flag) {
        if (flag) {
            btn_back.setVisibility(View.VISIBLE);
        } else {
            btn_back.setVisibility(View.INVISIBLE);
        }
    }


    protected void showClose(boolean flag) {
        if (flag) {
            btn_close.setVisibility(View.VISIBLE);
        } else {
            btn_close.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == ResourceUtil.getId(mContext, "btn_close")) {
            ConstantValue.ZY_SHOWBACK = false;
            if (UserModel.getInstance().getLogin_Token() != null) {
                ConstantValue.ZY_SHOWBACK = false;
                ZyGameSDK.getInstance().onLoginResult(UserModel.getInstance());
            }
            dismiss();
        }
    }


    public void showSoft() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        //加上下面这一行弹出对话框时软键盘随之弹出
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    protected void showHead(boolean isShow){
        if(isShow){
            rl_head.setVisibility(View.VISIBLE);
        }else {
            rl_head.setVisibility(View.GONE);
        }
    }

    public void showLoad(boolean isShow) {
        if (null != progressDialog) {
            if (isShow) {
                progressDialog.show();

            } else {
                progressDialog.dismiss();
            }
        }
    }

}
